package com.oa.admin.service.impl;

import com.joneying.common.annotation.CommonService;
import com.joneying.common.web.response.Resp;
import com.oa.admin.dto.MeetingApplyAddDTO;
import com.oa.admin.dto.MeetingApplyListDTO;
import com.oa.admin.vo.MeetingApplyDetailsVO;
import com.oa.admin.vo.MeetingApplyListVO;
import com.oa.core.PageResult;
import com.oa.admin.entity.MeetingApply;
import com.oa.admin.mapper.MeetingApplyMapper;
import com.oa.admin.service.IMeetingApplyService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.oa.core.enumm.ProcessDefineEnum;
import com.oa.core.exception.BusinessException;
import com.oa.core.exception.DataIsEmptyException;
import com.oa.system.dto.ApplyApprovalAddDTO;
import com.oa.system.service.IApplyApprovalService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 会议活动申请表 服务实现类
 * </p>
 *
 * @author syb123
 * @since 2019-01-22
 */
@Service
public class MeetingApplyServiceImpl extends ServiceImpl<MeetingApplyMapper, MeetingApply> implements IMeetingApplyService {

    private final MeetingApplyMapper meetingApplyMapper;

    @Autowired
    public MeetingApplyServiceImpl(MeetingApplyMapper meetingApplyMapper) {
        this.meetingApplyMapper = meetingApplyMapper;
    }

    @Autowired
    private IApplyApprovalService applyApprovalService;

    @Override
    @CommonService
    public PageResult<MeetingApply> findList(Integer pageNum, Integer pageSize) {
        if(pageNum == null){
            List<MeetingApply> list = meetingApplyMapper.findList();
            return new PageResult<>(list);
        }else{
            // 当前页，总条数 构造 page 对象
            Page<MeetingApply> page = new Page<>(pageNum, pageSize);
            //查询
            List<MeetingApply> list = meetingApplyMapper.findList(page);
            return new PageResult<>(list);
        }
    }

    /**
    * 根据id查询
    */
    @Override
    @CommonService
    public MeetingApplyDetailsVO findMeetingApplyById(String id){
        return meetingApplyMapper.findMeetingApplyDetails(id);
    }

    @Override
    @CommonService
    public PageResult<MeetingApplyListVO> findMeetingApplyList(MeetingApplyListDTO meetingApplyListDTO) {

        Page page = meetingApplyListDTO.getPage();
        return new PageResult(meetingApplyMapper.findMeetingApplyList(page, meetingApplyListDTO), page.getPages());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @CommonService
    public Resp add(MeetingApplyAddDTO meetingApplyAddDTO) throws BusinessException, DataIsEmptyException {
        MeetingApply meetingApply = new MeetingApply();
        BeanUtils.copyProperties(meetingApplyAddDTO, meetingApply);
        meetingApply.setApplyTime(new Date());
        if(insert(meetingApply)){
            ApplyApprovalAddDTO applyApprovalAddDTO = new ApplyApprovalAddDTO();
            applyApprovalAddDTO.setApplyId(meetingApply.getId());
            applyApprovalAddDTO.setApprovalProcessId(ProcessDefineEnum.CONFERENCE.getId());
            applyApprovalAddDTO.setApplyApprovalName(meetingApply.getCompanyName() + "来访会议" + ProcessDefineEnum.CONFERENCE.getText());
            applyApprovalAddDTO.setApprovalUserId(meetingApplyAddDTO.getProposer());
            return applyApprovalService.addApproval(applyApprovalAddDTO);
        }
        return new Resp().failure("添加申请失败");
    }
}
